package frame.options.simulation.results.virtualNetworks.graphical;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;

import model.components.links.link.BandwidthBackupFlag;
import model.components.links.link.Link;
import model.components.links.link.VirtualLink;
import model.components.links.link.PhysicalLink;
import model.components.networks.network.VirtualNetwork;
import model.components.networks.network.PhysicalNetwork;
import model.components.nodes.node.VirtualNode;
import model.components.nodes.node.Node;
import model.components.nodes.node.PhysicalNode;
import model.components.nodes.node.VirtualRouter;
import model.components.nodes.node.PhysicalRouter;
import model.components.nodes.node.VirtualServer;
import model.components.nodes.node.PhysicalServer;
import model.components.nodes.node.VirtualSwitch;
import model.components.nodes.node.PhysicalSwitch;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.AbstractPopupGraphMousePlugin;
import frame.options.simulation.results.ResultsFrame;
import frame.options.simulation.results.physicalNetworks.graphical.PhysicalNetworkVisualizationViewersPanel;

public class VirtualNetworkPopupGraphMousePlugin extends AbstractPopupGraphMousePlugin implements ActionListener
{
	public static final String ALL_LINK_REQUEST = "AllLinkRequest";
	public static final String ALL_LINK_REQUEST_NORMAL = "AllLinkRequestNormal";
	public static final String ALL_LINK_REQUEST_BACKUP = "AllLinkRequestBackup";
	
	public static final String ALL_NETWORK_REQUEST = "AllNetworkRequest";
	public static final String ALL_NETWORK_REQUEST_NORMAL = "AllNetworkRequestNormal";
	public static final String ALL_NETWORK_REQUEST_BACKUP = "AllNetworkRequestBackup";
	
	private VirtualNode nodeRequest;
	public VirtualNode getNodeRequest(){ return nodeRequest; }
	public void setNodeRequest( VirtualNode nodeRequest ){ this.nodeRequest = nodeRequest; }
	
	private JPopupMenu popupNodeRequest;
	public JPopupMenu getPopupNodeRequest(){ return popupNodeRequest; }
	public void setPopupNodeRequest( JPopupMenu popupNodeRequest ){ this.popupNodeRequest = popupNodeRequest; }
	
	private VirtualLink linkRequest;
	public VirtualLink getLinkRequest(){ return linkRequest; }
	public void setLinkRequest( VirtualLink linkRequest ){ this.linkRequest = linkRequest; }
	
	private JPopupMenu popupLinkRequest;
    public JPopupMenu getPopupLinkRequest(){ return popupLinkRequest; }
	public void setPopupLinkRequest( JPopupMenu popupLinkRequest ){ this.popupLinkRequest = popupLinkRequest; }
	
	private ResultsFrame frameResults;
	public ResultsFrame getResultsNetworks(){ return frameResults; }
	public void setResultsNetworks( ResultsFrame frameResults ){ this.frameResults = frameResults; }
	
	public VirtualNetworkPopupGraphMousePlugin( ResultsFrame frameResults )
    {
    	super( InputEvent.BUTTON3_MASK );
    	
    	this.setResultsNetworks( frameResults );
    	
    	this.setPopupNodeRequest( new JPopupMenu() );
    	this.setPopupLinkRequest( new JPopupMenu() );
    }
	
	@Override@SuppressWarnings("unchecked")
    protected void handlePopup( MouseEvent mouseEvent )
    {
		VisualizationViewer< Node, Link > visualizationViewer = ( VisualizationViewer< Node, Link > )mouseEvent.getSource();
	    	
		VirtualNode nodeRequest = ( VirtualNode )visualizationViewer.getPickSupport().getVertex( visualizationViewer.getGraphLayout(), mouseEvent.getPoint().getX(), mouseEvent.getPoint().getY() );
		this.setNodeRequest( nodeRequest );
	    	
		VirtualLink linkRequest = ( VirtualLink )visualizationViewer.getPickSupport().getEdge( visualizationViewer.getGraphLayout(), mouseEvent.getPoint().getX(), mouseEvent.getPoint().getY() );
		this.setLinkRequest( linkRequest );
	    	
		if( nodeRequest != null )
		{
			this.updatePopupNodeRequest( nodeRequest );
			this.getPopupNodeRequest().show( visualizationViewer, mouseEvent.getX(), mouseEvent.getY() );
		}
		else if( linkRequest != null )	
		{
			this.updatePopupLinkRequest( linkRequest );
			this.getPopupLinkRequest().show( visualizationViewer, mouseEvent.getX(), mouseEvent.getY() );
		}
    }
	
	private void updatePopupNodeRequest( VirtualNode nodeRequest )
    {
		this.getPopupNodeRequest().removeAll();
		this.getPopupNodeRequest().add( this.menuItemNodeRequest( nodeRequest ) );
		
		if( nodeRequest.getPhysicalNode() != null )
		{
			this.getPopupNodeRequest().addSeparator();
			this.getPopupNodeRequest().add( this.menuItemNodeSubstrate( nodeRequest ) );
			
			this.getPopupNodeRequest().addSeparator();
			this.getPopupNodeRequest().add( this.menuItemAllNetwork( nodeRequest ) );
		}
    }
	private JMenuItem menuItemNodeRequest( VirtualNode nodeRequest )
	{
		Icon iconMenuItemNodeRequest = null;
		
		if( nodeRequest.isSelected() )
			iconMenuItemNodeRequest = nodeRequest.getGreenIcon();
		else if( nodeRequest.getPhysicalNode() != null )
			iconMenuItemNodeRequest = nodeRequest.getYellowIcon();
		else
			iconMenuItemNodeRequest = nodeRequest.getGreyIcon();
    	
		String textMenuItemNodeRequest = null;
		
    	if( nodeRequest instanceof VirtualRouter || nodeRequest instanceof VirtualSwitch )
    		textMenuItemNodeRequest = "<html><b>NODE:</b> " + nodeRequest.getName() + "<br><b>NETWORK:</b> " + nodeRequest.getNetworkName() + "<br><b>OPERATING SYSTEM:</b> " + nodeRequest.getOperatingSystem() + "<br><b>VIRTUAL ENVIRONMENT:</b> " + nodeRequest.getVirtualEnvironment() + "<br><b>CPU:</b> " + nodeRequest.getCpu() + "<br><b>MEMORY:</b> " + nodeRequest.getMemory() + "</html>";
    	else if( nodeRequest instanceof VirtualServer )
    		textMenuItemNodeRequest = "<html><b>NODE:</b> " + nodeRequest.getName() + "<br><b>NETWORK:</b> " + nodeRequest.getNetworkName() + "<br><b>OPERATING SYSTEM:</b> " + nodeRequest.getOperatingSystem() + "<br><b>VIRTUAL ENVIRONMENT:</b> " + nodeRequest.getVirtualEnvironment() + "<br><b>CPU:</b> " + nodeRequest.getCpu() + "<br><b>MEMORY:</b> " + nodeRequest.getMemory() + "<br><b>DISK SPACE:</b> " + nodeRequest.getDiskSpace() + "</html>";
		
    	JMenuItem menuItemNodeRequest = new JMenuItem();
    	menuItemNodeRequest.setIcon( iconMenuItemNodeRequest );
    	menuItemNodeRequest.setText( textMenuItemNodeRequest );
    	menuItemNodeRequest.addActionListener( this );
    	
    	return menuItemNodeRequest;
	}
	private JMenuItem menuItemNodeSubstrate( VirtualNode nodeRequest )
	{
		Icon iconMenuItemNodeSubstrate = null;
		
		PhysicalNode nodeSubstrate = nodeRequest.getPhysicalNode();
		
		if( nodeRequest.isSelected() )
			iconMenuItemNodeSubstrate = nodeSubstrate.getGreenIcon();
		else
			iconMenuItemNodeSubstrate = nodeSubstrate.getYellowIcon();
		
		String textMenuItemNodeSubstrate = null;
		
		if( nodeSubstrate instanceof PhysicalRouter || nodeSubstrate instanceof PhysicalSwitch )
			textMenuItemNodeSubstrate = "<html><b>NODE:</b> " + nodeSubstrate.getName() + "<br><b>NETWORK:</b> " + nodeSubstrate.getNetworkName() + "<br><b>OPERATING SYSTEM:</b> " + nodeRequest.getOperatingSystem() + "<br><b>VIRTUAL ENVIRONMENT:</b> " + nodeRequest.getVirtualEnvironment() + "<br><b>CPU:</b> " + nodeSubstrate.getCpu() + "<br><b>MEMORY:</b> " + nodeSubstrate.getMemory() + "</html>";
		else if( nodeSubstrate instanceof PhysicalServer )
			textMenuItemNodeSubstrate = "<html><b>NODE:</b> " + nodeSubstrate.getName() + "<br><b>NETWORK:</b> " + nodeSubstrate.getNetworkName() + "<br><b>OPERATING SYSTEM:</b> " + nodeRequest.getOperatingSystem() + "<br><b>VIRTUAL ENVIRONMENT:</b> " + nodeRequest.getVirtualEnvironment() + "<br><b>CPU:</b> " + nodeSubstrate.getCpu() + "<br><b>MEMORY:</b> " + nodeSubstrate.getMemory() + "<br><b>DISK SPACE:</b> " + nodeSubstrate.getDiskSpace() + "</html>";
		
		JMenuItem menuItemNodeSubstrate = new JMenuItem();
		menuItemNodeSubstrate.setIcon( iconMenuItemNodeSubstrate );
		menuItemNodeSubstrate.setText( textMenuItemNodeSubstrate );
		menuItemNodeSubstrate.addActionListener( this );
        	
		return menuItemNodeSubstrate;
	}
	private JMenuItem menuItemAllNetwork( VirtualNode nodeRequest )
	{
		JMenuItem menuItemAllNetwork = new JMenuItem();
		menuItemAllNetwork.setName( ALL_NETWORK_REQUEST );
		menuItemAllNetwork.setIcon( nodeRequest.getGreyIcon() );
		menuItemAllNetwork.setText( "<html><b>ALL SERVICE REQUEST</b></html>" );
		menuItemAllNetwork.addActionListener( this );
		
		return menuItemAllNetwork;
	}
	
	private void updatePopupLinkRequest( VirtualLink linkRequest )
	{
		VirtualNode nodeRequestFirst = (VirtualNode) linkRequest.getFirstNode();
		VirtualNode nodeRequestSecond = (VirtualNode) linkRequest.getSecondNode();
		
		PhysicalNode nodeSubstrateFirst = nodeRequestFirst.getPhysicalNode();
		PhysicalNode nodeSubstrateSecond = nodeRequestSecond.getPhysicalNode();
		
		this.getPopupLinkRequest().removeAll();
		this.getPopupLinkRequest().add( this.menuItemLinkRequest( linkRequest ) );
			
    	if( linkRequest.getPhysicalLinksBandwidthsBackupFlags().size() == 0 && nodeSubstrateFirst != null && nodeSubstrateSecond != null  )
    	{
	    	this.getPopupLinkRequest().addSeparator();
			this.getPopupLinkRequest().add( this.menuItemLinkSubstrate( null, linkRequest ) );
    	}
    	else if( linkRequest.getPhysicalLinksBandwidthsBackupFlags().size() != 0 )
    	{
	    	LinkedHashMap< PhysicalLink, BandwidthBackupFlag > linksSubstrate       = linkRequest.getPhysicalLinksBandwidthsBackupFlags();
	    	LinkedHashMap< PhysicalLink, BandwidthBackupFlag > linksSubstrateNormal = new LinkedHashMap< PhysicalLink, BandwidthBackupFlag >();
	    	LinkedHashMap< PhysicalLink, BandwidthBackupFlag > linksSubstrateBackup = new LinkedHashMap< PhysicalLink, BandwidthBackupFlag >();
	    	
			this.getPopupLinkRequest().addSeparator();
			this.getPopupLinkRequest().add( this.menuItemAllNetwork( linkRequest ) );
	    	
	    	if( linksSubstrate.size() != 0 )
	    	{
		    	Iterator< Entry< PhysicalLink, BandwidthBackupFlag > > iterator = linkRequest.getPhysicalLinksBandwidthsBackupFlags().entrySet().iterator();
				
		    	while( iterator.hasNext() )
				{
					Entry< PhysicalLink, BandwidthBackupFlag > entry = iterator.next();
					
					if( entry.getValue().getBackupFlag() == 0 )
						linksSubstrateNormal.put( entry.getKey(), entry.getValue() );
					
					else if( entry.getValue().getBackupFlag() == 1 )
						linksSubstrateBackup.put( entry.getKey(), entry.getValue() );
				}
	    	}
	    	
	    	if( linksSubstrateNormal.size() != 0 )
	    	{
	    		this.getPopupLinkRequest().addSeparator();
	    		this.getPopupLinkRequest().add( this.menuItemLinkRequestNormal( linkRequest ) );
	    		
	    		Iterator< Entry< PhysicalLink, BandwidthBackupFlag > > iterator = linksSubstrateNormal.entrySet().iterator();
	    		
				while( iterator.hasNext() )
				{
					this.getPopupLinkRequest().addSeparator();
					this.getPopupLinkRequest().add( this.menuItemLinkSubstrate( iterator.next(), linkRequest ) );
				}
				
				this.getPopupLinkRequest().addSeparator();
				this.getPopupLinkRequest().add( this.menuItemAllNetworkNormal( linkRequest ) );
	    	}
	    	
	    	if( linksSubstrateBackup.size() != 0 )
	    	{
	    		this.getPopupLinkRequest().addSeparator();
	    		this.getPopupLinkRequest().add( this.menuItemLinkRequestBackup( linkRequest ) );
	    		
	    		Iterator< Entry< PhysicalLink, BandwidthBackupFlag > > iterator = linksSubstrateBackup.entrySet().iterator();
	    		
				while( iterator.hasNext() )
				{
					this.getPopupLinkRequest().addSeparator();
					this.getPopupLinkRequest().add( this.menuItemLinkSubstrate( iterator.next(), linkRequest ) );
				}
				
				this.getPopupLinkRequest().addSeparator();
				this.getPopupLinkRequest().add( this.menuItemAllNetworkBackup( linkRequest ) );
	    	}
    	}
	}
	private JMenuItem menuItemLinkRequest( VirtualLink linkRequest )
	{
		Node nodeFirst = linkRequest.getFirstNode();
    	Node nodeSecond = linkRequest.getSecondNode();
		
    	JMenuItem menuItemLinkRequest = new JMenuItem();
    	menuItemLinkRequest.setText( "<html><b>LINK:</b> " + nodeFirst.getName() + " <b>to</b> " + nodeSecond.getName() + "<br><b>NETWORK:</b> " + linkRequest.getNetworkName() +  "<br><b>BANDWIDTH: </b> " + linkRequest.getBandwidth() + "<br><b>TYPE: </b> " + linkRequest.getType() + "</html>" );
    	menuItemLinkRequest.setName( ALL_LINK_REQUEST );
    	menuItemLinkRequest.setIcon( linkRequest.getIcon() );
    	menuItemLinkRequest.addActionListener( this );
    	
    	return menuItemLinkRequest;
	}
	private JMenuItem menuItemAllNetwork( VirtualLink linkRequest )
	{
		JMenuItem menuItemAllNetwork = new JMenuItem();
		menuItemAllNetwork.setName( ALL_NETWORK_REQUEST );
		menuItemAllNetwork.setIcon( linkRequest.getIcon() );
		menuItemAllNetwork.setText( "<html><b>ALL SERVICE REQUEST</b></html>" );
		menuItemAllNetwork.addActionListener( this );
		
		return menuItemAllNetwork;
	}
	private JMenuItem menuItemLinkRequestNormal( VirtualLink linkRequest )
	{
		Iterator< Entry< PhysicalLink, BandwidthBackupFlag > > iterator = linkRequest.getPhysicalLinksBandwidthsBackupFlags().entrySet().iterator();
		double distanceTotal = 0;
		
		while( iterator.hasNext() )
		{
			Entry< PhysicalLink, BandwidthBackupFlag > entry = iterator.next();
			
			if( entry.getValue().getBackupFlag() == 0 )
				distanceTotal += entry.getKey().getDistance();
		}
		
		JMenuItem menuItemLinkRequestNormal = new JMenuItem();
    	menuItemLinkRequestNormal.setText( "<html><b>BANDWIDTH: </b> " + linkRequest.getBandwidth() + " Normal" + "<br><b>DISTANCE: </b> " + distanceTotal + "</html>" );
    	menuItemLinkRequestNormal.setName( ALL_LINK_REQUEST_NORMAL );
    	menuItemLinkRequestNormal.setIcon( linkRequest.getIcon() );
    	menuItemLinkRequestNormal.addActionListener( this );
    	
    	return menuItemLinkRequestNormal;
	}
	private JMenuItem menuItemAllNetworkNormal( VirtualLink linkRequest )
	{
		JMenuItem menuItemAllNetwork = new JMenuItem();
		menuItemAllNetwork.setName( ALL_NETWORK_REQUEST_NORMAL );
		menuItemAllNetwork.setIcon( linkRequest.getIcon() );
		menuItemAllNetwork.setText( "<html><b>ALL NORMAL SERVICE REQUEST</b></html>" );
		menuItemAllNetwork.addActionListener( this );
		
		return menuItemAllNetwork;
	}
	private JMenuItem menuItemLinkRequestBackup( VirtualLink linkRequest )
	{
		Iterator< Entry< PhysicalLink, BandwidthBackupFlag > > iterator = linkRequest.getPhysicalLinksBandwidthsBackupFlags().entrySet().iterator();
		double distanceTotal = 0;
		
		while( iterator.hasNext() )
		{
			Entry< PhysicalLink, BandwidthBackupFlag > entry = iterator.next();
			
			if( entry.getValue().getBackupFlag() == 1 )
				distanceTotal += entry.getKey().getDistance();
		}
		
		JMenuItem menuItemLinkRequestBackup = new JMenuItem();
    	menuItemLinkRequestBackup.setText( "<html><b>BANDWIDTH: </b> " + linkRequest.getBandwidth() + " Backup" + "<br><b>DISTANCE: </b> " + distanceTotal + "</html>" );
    	menuItemLinkRequestBackup.setName( ALL_LINK_REQUEST_BACKUP );
    	menuItemLinkRequestBackup.setIcon( linkRequest.getIcon() );
    	menuItemLinkRequestBackup.addActionListener( this );
    	
    	return menuItemLinkRequestBackup;
	}
	private JMenuItem menuItemAllNetworkBackup( VirtualLink linkRequest )
	{
		JMenuItem menuItemAllNetwork = new JMenuItem();
		menuItemAllNetwork.setName( ALL_NETWORK_REQUEST_BACKUP );
		menuItemAllNetwork.setIcon( linkRequest.getIcon() );
		menuItemAllNetwork.setText( "<html><b>ALL BACKUP SERVICE REQUEST</b></html>" );
		menuItemAllNetwork.addActionListener( this );
		
		return menuItemAllNetwork;
	}
	private JMenuItem menuItemLinkSubstrate( Entry< PhysicalLink, BandwidthBackupFlag > entry, VirtualLink linkRequest )
	{	
		VirtualNode nodeRequestFirst = (VirtualNode) linkRequest.getFirstNode();
    	VirtualNode nodeRequestSecond = (VirtualNode) linkRequest.getSecondNode();
		
    	PhysicalNode nodeSubstrateFirst = nodeRequestFirst.getPhysicalNode();
    	PhysicalNode nodeSubstrateSecond = nodeRequestSecond.getPhysicalNode();
		
    	Icon icon = null;
		String name = null;
		String text = null;
		JMenuItem menuItemLinkSubstrate = new JMenuItem();
		
    	if( entry != null )
    	{
    		PhysicalNetwork physicalNetwork = this.networkSubstrateTime2( entry.getKey().getNetworkName(), linkRequest.getTimeStamp() );
    		
    		PhysicalLink linkSubstrate = null;
    		
    		for( PhysicalLink linkSub: physicalNetwork.getPhysicalLinks() )
    			if( linkSub.getNumber() == entry.getKey().getNumber() )
    				linkSubstrate = linkSub;
    		
			PhysicalLink linkSubstrateTime = entry.getKey();
			PhysicalNode nodeSubFirstAlloc = (PhysicalNode) linkSubstrateTime.getFirstNode();
			PhysicalNode nodeSubSecondAlloc = (PhysicalNode) linkSubstrateTime.getSecondNode();
			icon = linkSubstrateTime.getIcon();
			name = linkSubstrateTime.getName() + "-" + linkSubstrateTime.getNetworkName();
			
			text = "<html><b>LINK:</b> " + nodeSubFirstAlloc.getName();
			
			if( nodeSubstrateFirst.getNumber() == nodeSubFirstAlloc.getNumber() )
				text += " (<FONT COLOR=\"green\">" + nodeRequestFirst.getName() + "</FONT>)";
			else if( nodeSubstrateSecond.getNumber() == nodeSubFirstAlloc.getNumber() )
				text += " (<FONT COLOR=\"green\">" + nodeRequestSecond.getName() + "</FONT>)";
			
			text += " <b>to</b> " + nodeSubSecondAlloc.getName();
			
			if( nodeSubstrateFirst.getNumber() == nodeSubSecondAlloc.getNumber() )
				text += " (<FONT COLOR=\"green\">" + nodeRequestFirst.getName() + "</FONT>)";
			else if( nodeSubstrateSecond.getNumber() == nodeSubSecondAlloc.getNumber() )
				text += " (<FONT COLOR=\"green\">" + nodeRequestSecond.getName() + "</FONT>)";
						
			if( entry.getValue().getBackupFlag() == 0 )
				text += "<br><b>NETWORK:</b> " + linkSubstrateTime.getNetworkName() + "<br><b>BANDWIDTH:</b> " + linkSubstrateTime.getBandwidth() + " (<FONT COLOR=\"green\">" + entry.getValue().getBandwidth() + " Normal</FONT>)" + "<br><b>DISTANCE:</b> " + linkSubstrateTime.getDistance() + "<br><b>AVAILABLE BANDWIDTH:</b> " + linkSubstrate.getAvailableBandwidth() + "</html>";
			else if( entry.getValue().getBackupFlag() == 1 )
				text += "<br><b>NETWORK:</b> " + linkSubstrateTime.getNetworkName() + "<br><b>BANDWIDTH:</b> " + linkSubstrateTime.getBandwidth() + " (<FONT COLOR=\"green\">" + entry.getValue().getBandwidth() + " Backup</FONT>)" + "<br><b>DISTANCE:</b> " + linkSubstrateTime.getDistance() + "<br><b>AVAILABLE BANDWIDTH:</b> " + linkSubstrate.getAvailableBandwidth() + "</html>";
			
			menuItemLinkSubstrate.addActionListener( this );
    	}
		else
		{
			icon = new ImageIcon( this.getClass().getResource(  "/icons/physicalLink.png" ) );
			text = "<html><b>Link between</b> " + nodeSubstrateFirst.getNetworkName() + " <b>and</b> " + nodeSubstrateSecond.getNetworkName() + "</html>";
		}
    	
		menuItemLinkSubstrate.setIcon( icon );
		menuItemLinkSubstrate.setName( name );
    	menuItemLinkSubstrate.setText( text );
    	
		return menuItemLinkSubstrate;
	}
	
	@Override
	public void actionPerformed( ActionEvent actionEvent )
	{	
		this.unSelectNetworksSubstrate();
		this.unSelectNetworksRequest();
		
		VirtualNode nodeRequest = this.getNodeRequest();
		VirtualLink linkRequest = this.getLinkRequest();
		
		String nameMenuItem = ( (JMenuItem) actionEvent.getSource() ).getName();
						
		if( nameMenuItem != null && nameMenuItem.equals( ALL_NETWORK_REQUEST ) )
		{
			String nameNetworkRequest = null;
			String nameNetworkSubstrate = null;
			int time = -1;
			
			if( nodeRequest != null && nodeRequest.getPhysicalNode() != null )
			{
				nameNetworkRequest = nodeRequest.getNetworkName();
				nameNetworkSubstrate = nodeRequest.getPhysicalNode().getNetworkName();
				time = nodeRequest.getTimeStamp();
			}
			else if( linkRequest != null && linkRequest.getPhysicalLinksBandwidthsBackupFlags().size() > 0 )
			{
				nameNetworkRequest = linkRequest.getNetworkName();
				nameNetworkSubstrate = linkRequest.getPhysicalLinksBandwidthsBackupFlags().entrySet().iterator().next().getKey().getNetworkName();
				time = linkRequest.getTimeStamp();
			}
			
			VirtualNetwork virtualNetwork = this.networkRequestTime( nameNetworkRequest, time );
				
			for( VirtualNode nodeRequestResult: virtualNetwork.getVirtualNodes() )
				if( nodeRequestResult.getPhysicalNode().getNetworkName().equals( nameNetworkSubstrate ) )
					nodeRequestResult.setSelected( true );
					
			for( VirtualLink linkRequestResult: virtualNetwork.getVirtualLinks() )
				if( linkRequestResult.getPhysicalLinksBandwidthsBackupFlags().size() > 0 && nameNetworkSubstrate.equals( linkRequestResult.getPhysicalLinksBandwidthsBackupFlags().entrySet().iterator().next().getKey().getNetworkName() ) )
				{
					linkRequestResult.setSelected( true );
					linkRequestResult.getFirstNode().setSelected( true );
					linkRequestResult.getSecondNode().setSelected( true );
				}
			
			PhysicalNetwork physicalNetwork = this.networkSubstrateTime( nameNetworkSubstrate, time );
			
			if( nodeRequest != null )
				for( PhysicalNode physicalNode: physicalNetwork.getPhysicalNodes() )
					if( physicalNode.getNumber() == nodeRequest.getPhysicalNode().getNumber() )
					{
						physicalNode.setSelected( true );
						
						for( VirtualNode nodeRequestSubstrate: physicalNode.getVirtualNodes() )
							if( nodeRequest.getNumber() == nodeRequestSubstrate.getNumber() )
							{
								nodeRequestSubstrate.setSelected( true );
								break;
							}
						
						break;
					}
			
			for( PhysicalLink linkSubstrate: physicalNetwork.getPhysicalLinks() )
			{
				Iterator< Entry< VirtualLink, BandwidthBackupFlag > > iterator = linkSubstrate.getVirtualLinksBandwidthsBackupFlags().entrySet().iterator();
				
				while( iterator.hasNext() )
				{
					VirtualLink linkRequestSubstrate = iterator.next().getKey();
					
					if( nameNetworkRequest.equals( linkRequestSubstrate.getNetworkName() ) )
					{
						linkSubstrate.setSelected( true );
						linkSubstrate.getFirstNode().setSelected( true );
						linkSubstrate.getSecondNode().setSelected( true );
						
						linkRequestSubstrate.setSelected( true );
						break;
					}
				}
			}
		}
		else if( this.getNodeRequest() != null )
		{
			nodeRequest.setSelected( true );
			
			if( nodeRequest.getPhysicalNode() != null )
			{
				PhysicalNode nodeSubstrateRequest = nodeRequest.getPhysicalNode();
				String nameNetworkSubstrate = nodeSubstrateRequest.getNetworkName();
				int time = nodeRequest.getTimeStamp();
				
				PhysicalNetwork physicalNetwork = this.networkSubstrateTime( nameNetworkSubstrate, time );
					
				for( PhysicalNode nodeSubstrate: physicalNetwork.getPhysicalNodes() )
					if( nodeSubstrate.getNumber() == nodeSubstrateRequest.getNumber() )
					{
						nodeSubstrate.setSelected( true );
						
						for( VirtualNode nodeRequestSubstrate: nodeSubstrate.getVirtualNodes() )
							if( nodeRequest.getNumber() == nodeRequestSubstrate.getNumber() )
							{
								nodeRequestSubstrate.setSelected( true );
								break;
							}
							
						break;
					}
			}
		}
		else if( this.getLinkRequest() != null )
		{
			linkRequest.setSelected( true );
			linkRequest.getFirstNode().setSelected( true );
			linkRequest.getSecondNode().setSelected( true );
			
			if( linkRequest.getPhysicalLinksBandwidthsBackupFlags().size() > 0 )
			{
				String nameNetworkSubstrate = linkRequest.getPhysicalLinksBandwidthsBackupFlags().entrySet().iterator().next().getKey().getNetworkName();
				int time = linkRequest.getTimeStamp();
				PhysicalNetwork physicalNetwork = this.networkSubstrateTime( nameNetworkSubstrate, time );
				
				if( nameMenuItem.equals( ALL_LINK_REQUEST ) )
				{
					Iterator< Entry< PhysicalLink, BandwidthBackupFlag > > iterator = linkRequest.getPhysicalLinksBandwidthsBackupFlags().entrySet().iterator();
					
					while( iterator.hasNext() )
					{
						PhysicalLink linkSubstrateRequest = iterator.next().getKey();
					
						for( PhysicalLink linkSubstrate: physicalNetwork.getPhysicalLinks() )
							if( linkSubstrate.getNumber() == linkSubstrateRequest.getNumber() )
							{
								linkSubstrate.setSelected( true );
								linkSubstrate.getFirstNode().setSelected( true );
								linkSubstrate.getSecondNode().setSelected( true );
								break;
							}
					}
				}
				else if( nameMenuItem.equals( ALL_LINK_REQUEST_NORMAL ) )
				{
					Iterator< Entry< PhysicalLink, BandwidthBackupFlag > > iterator = linkRequest.getPhysicalLinksBandwidthsBackupFlags().entrySet().iterator();
					
					while( iterator.hasNext() )
					{
						Entry< PhysicalLink, BandwidthBackupFlag > entry = iterator.next();
						PhysicalLink linkSubstrateRequest = entry.getKey();
						int type = entry.getValue().getBackupFlag(); 
						
						if( type  == 0 )
							for( PhysicalLink linkSubstrate: physicalNetwork.getPhysicalLinks() )
								if( linkSubstrate.getNumber() == linkSubstrateRequest.getNumber() )
								{
									linkSubstrate.setSelected( true );
									linkSubstrate.getFirstNode().setSelected( true );
									linkSubstrate.getSecondNode().setSelected( true );
									break;
								}
					}
				}
				else if( nameMenuItem != null && nameMenuItem.equals( ALL_NETWORK_REQUEST_NORMAL ) )
				{
					String nameNetworkRequest = linkRequest.getNetworkName();
					VirtualNetwork virtualNetwork = networkRequestTime( nameNetworkRequest, time );
						
					for( VirtualNode nodeRequestResult: virtualNetwork.getVirtualNodes() )
						if( nodeRequestResult.getPhysicalNode().getNetworkName().equals( nameNetworkSubstrate ) )
							nodeRequestResult.setSelected( true );
							
					for( VirtualLink linkRequestResult: virtualNetwork.getVirtualLinks() )
						if( linkRequestResult.getPhysicalLinksBandwidthsBackupFlags().size() > 0 && nameNetworkSubstrate.equals( linkRequestResult.getPhysicalLinksBandwidthsBackupFlags().entrySet().iterator().next().getKey().getNetworkName() ) )
						{
							linkRequestResult.setSelected( true );
							linkRequestResult.getFirstNode().setSelected( true );
							linkRequestResult.getSecondNode().setSelected( true );
						}
					
					for( PhysicalLink linkSubstrate: physicalNetwork.getPhysicalLinks() )
					{
						Iterator< Entry< VirtualLink, BandwidthBackupFlag > > iterator = linkSubstrate.getVirtualLinksBandwidthsBackupFlags().entrySet().iterator();
						
						while( iterator.hasNext() )
						{
							Entry< VirtualLink, BandwidthBackupFlag > entry = iterator.next();
							VirtualLink linkRequestSubstrate = entry.getKey();
							int type = entry.getValue().getBackupFlag();
							
							if( type == 0 && nameNetworkRequest.equals( linkRequestSubstrate.getNetworkName() ) )
							{
								linkSubstrate.setSelected( true );
								linkSubstrate.getFirstNode().setSelected( true );
								linkSubstrate.getSecondNode().setSelected( true );
								
								linkRequestSubstrate.setSelected( true );
								break;
							}
						}
					}
				}
				else if( nameMenuItem.equals( ALL_LINK_REQUEST_BACKUP ) )
				{
					Iterator< Entry< PhysicalLink, BandwidthBackupFlag > > iterator = linkRequest.getPhysicalLinksBandwidthsBackupFlags().entrySet().iterator();
					
					while( iterator.hasNext() )
					{
						Entry< PhysicalLink, BandwidthBackupFlag > entry = iterator.next();
						PhysicalLink linkSubstrateRequest = entry.getKey();
						int type = entry.getValue().getBackupFlag(); 
						
						if( type  == 1 )
							for( PhysicalLink linkSubstrate: physicalNetwork.getPhysicalLinks() )
								if( linkSubstrate.getNumber() == linkSubstrateRequest.getNumber() )
								{
									linkSubstrate.setSelected( true );
									linkSubstrate.getFirstNode().setSelected( true );
									linkSubstrate.getSecondNode().setSelected( true );
									break;
								}
					}
				}
				else if( nameMenuItem != null && nameMenuItem.equals( ALL_NETWORK_REQUEST_BACKUP ) )
				{
					String nameNetworkRequest = linkRequest.getNetworkName();
					VirtualNetwork virtualNetwork = networkRequestTime( nameNetworkRequest, time );
						
					for( VirtualNode nodeRequestResult: virtualNetwork.getVirtualNodes() )
						if( nodeRequestResult.getPhysicalNode().getNetworkName().equals( nameNetworkSubstrate ) )
							nodeRequestResult.setSelected( true );
							
					for( VirtualLink linkRequestResult: virtualNetwork.getVirtualLinks() )
						if( linkRequestResult.getPhysicalLinksBandwidthsBackupFlags().size() > 0 && nameNetworkSubstrate.equals( linkRequestResult.getPhysicalLinksBandwidthsBackupFlags().entrySet().iterator().next().getKey().getNetworkName() ) )
						{
							linkRequestResult.setSelected( true );
							linkRequestResult.getFirstNode().setSelected( true );
							linkRequestResult.getSecondNode().setSelected( true );
						}
					
					for( PhysicalLink linkSubstrate: physicalNetwork.getPhysicalLinks() )
					{
						Iterator< Entry< VirtualLink, BandwidthBackupFlag > > iterator = linkSubstrate.getVirtualLinksBandwidthsBackupFlags().entrySet().iterator();
						
						while( iterator.hasNext() )
						{
							Entry< VirtualLink, BandwidthBackupFlag > entry = iterator.next();
							VirtualLink linkRequestSubstrate = entry.getKey();
							int type = entry.getValue().getBackupFlag();
							
							if( type == 1 && nameNetworkRequest.equals( linkRequestSubstrate.getNetworkName() ) )
							{
								linkSubstrate.setSelected( true );
								linkSubstrate.getFirstNode().setSelected( true );
								linkSubstrate.getSecondNode().setSelected( true );
								
								linkRequestSubstrate.setSelected( true );
								break;
							}
						}
					}
				}
				else
				{
					for( PhysicalLink linkSubstrate: physicalNetwork.getPhysicalLinks() )
						if( linkSubstrate.getName().equals( nameMenuItem.split( "-" )[ 0 ] ) && linkSubstrate.getNetworkName().equals( nameMenuItem.split( "-" )[ 1 ] ) )
						{
							linkSubstrate.setSelected( true );
							linkSubstrate.getFirstNode().setSelected( true );
							linkSubstrate.getSecondNode().setSelected( true );
							break;
						}
				}
			}
		}
		
		this.getResultsNetworks().getVirtualNetworksVisualizationViewersPanelsTabbedPane().repaint();
		this.getResultsNetworks().getPhysicalNetworksVisualizationViewersPanelsTabbedPane().repaint();
	}
	
	private void unSelectNetworksSubstrate()
	{
		List< PhysicalNetworkVisualizationViewersPanel > graphicsNetworksSubstrate = this.getResultsNetworks().getPhysicalNetworksVisualizationViewersPanels();
		
		for( int i = 0; i < graphicsNetworksSubstrate.size(); i++ )
		{
			List< PhysicalNetwork > networksSubstrate = graphicsNetworksSubstrate.get( i ).getPhysicalNetworks();
			
			for( int j = 0; j < networksSubstrate.size(); j++ )
			{
				PhysicalNetwork physicalNetwork = networksSubstrate.get( j );
				
				for( PhysicalNode nodeSubstrate: physicalNetwork.getPhysicalNodes() )
				{
					nodeSubstrate.setSelected( false );
					
					for( VirtualNode nodeRequest: nodeSubstrate.getVirtualNodes() )
						nodeRequest.setSelected( false );
				}
				
				for( PhysicalLink linkSubstrate: physicalNetwork.getPhysicalLinks() )
					linkSubstrate.setSelected( false );
			}
		}
	}
	private void unSelectNetworksRequest()
	{
		List< VirtualNetworkVisualizationViewersPanel > graphicsNetworksRequest = this.getResultsNetworks().getVirtualNetworksVisualizationViewersPanels();
		
		for( int i = 0; i < graphicsNetworksRequest.size(); i++ )
		{
			List< VirtualNetwork > networksRequest = graphicsNetworksRequest.get( i ).getVirtualNetworks();
			
			for( int j = 0; j < networksRequest.size(); j++ )
			{
				VirtualNetwork virtualNetwork = networksRequest.get( j );
				
				for( VirtualNode nodeRequest: virtualNetwork.getVirtualNodes() )
					nodeRequest.setSelected( false );
				
				for( VirtualLink linkRequest: virtualNetwork.getVirtualLinks() )
					linkRequest.setSelected( false );
			}
		}
	}
	
	private PhysicalNetwork networkSubstrateTime( String nameNetworkSubstrate, int time )
	{
		JTabbedPane tabbedPaneVisualizationViewersNetworksSubstrate = this.getResultsNetworks().getPhysicalNetworksVisualizationViewersPanelsTabbedPane();
		int indexTabbedPaneVisualizationViewersNetworksSubstrate = tabbedPaneVisualizationViewersNetworksSubstrate.indexOfTab( nameNetworkSubstrate );
		PhysicalNetworkVisualizationViewersPanel panelVisualizationViewersNetworkSubstrate = this.getResultsNetworks().getPhysicalNetworksVisualizationViewersPanels().get( indexTabbedPaneVisualizationViewersNetworksSubstrate );
		List< PhysicalNetwork > networksSubstrateTime = panelVisualizationViewersNetworkSubstrate.getPhysicalNetworks();
		
		for( int i = 0; i < networksSubstrateTime.size(); i++ )
		{
			PhysicalNetwork networkSubstrateTime = networksSubstrateTime.get( i );
			
			if( networkSubstrateTime.getTimeStamp() == time )
			{
				tabbedPaneVisualizationViewersNetworksSubstrate.setSelectedIndex( indexTabbedPaneVisualizationViewersNetworksSubstrate );
				panelVisualizationViewersNetworkSubstrate.putValueSliderss( i );
				
				return networkSubstrateTime;
			}
		}
		
		return null;
	}
	private PhysicalNetwork networkSubstrateTime2( String nameNetworkSubstrate, int time )
	{
		JTabbedPane tabbedPaneVisualizationViewersNetworksSubstrate = this.getResultsNetworks().getPhysicalNetworksVisualizationViewersPanelsTabbedPane();
		int indexTabbedPaneVisualizationViewersNetworksSubstrate = tabbedPaneVisualizationViewersNetworksSubstrate.indexOfTab( nameNetworkSubstrate );
		PhysicalNetworkVisualizationViewersPanel panelVisualizationViewersNetworkSubstrate = this.getResultsNetworks().getPhysicalNetworksVisualizationViewersPanels().get( indexTabbedPaneVisualizationViewersNetworksSubstrate );
		List< PhysicalNetwork > networksSubstrateTime = panelVisualizationViewersNetworkSubstrate.getPhysicalNetworks();
			
		for( int i = 0; i < networksSubstrateTime.size(); i++ )
			if( networksSubstrateTime.get( i ).getTimeStamp() == time )
				return networksSubstrateTime.get( i );
		
		return null;
	}
	
	private VirtualNetwork networkRequestTime( String nameNetworkRequest, int time )
	{
		JTabbedPane panesGraphsNetworksRequest = this.getResultsNetworks().getVirtualNetworksVisualizationViewersPanelsTabbedPane();
		int indexNetworkRequest = panesGraphsNetworksRequest.indexOfTab( nameNetworkRequest );
		panesGraphsNetworksRequest.setSelectedIndex( indexNetworkRequest );
		
		VirtualNetworkVisualizationViewersPanel panelVisualizationViewersNetworkRequest = this.getResultsNetworks().getVirtualNetworksVisualizationViewersPanels().get( indexNetworkRequest );
		List< VirtualNetwork > virtualNetwork = panelVisualizationViewersNetworkRequest.getVirtualNetworks();
			
		for( int i = 0; i < virtualNetwork.size(); i++ )
			if( virtualNetwork.get( i ).getTimeStamp() == time )
				return virtualNetwork.get( i );
		
		return null;
	}

}
